const BaseRest = require('../rest.js');
module.exports = class extends BaseRest
{
    constructor(ctx)
    {
        super(ctx);
        this._judgerModel = this.model('apiv1/judger');
        this._judgerService = this.service('apiv1/judger');
        this.competitionsteptoworksModel = this.model('apiv1/competitionsteptoworks');
        this.stepsModel = this.model('apiv1/competitionSteps');
    }

    async postAction()
    {
        let postDataObj = think.stringToJson(this.post('data'));
        if (postDataObj === false)
        {
            return this.error(52001, "提交的表单信息错误,请检查数据格式,上传数据格式为{'data',yourJsonData},如 : $.post(\"http://127.0.0.1:8091/apiv1/competition\",{'data':yourJsonData}),yourJsonData为你要上传的数据包");
        }
        let service = this._judgerService.getJudgerData(postDataObj);
        if (service.isErr === false)
        {
            let dbRes = await this._judgerService.addData();
            return this.error(dbRes.errNo, dbRes.errMsg);
        }
        return this.error(service.errNo, service.errMsg);
    }


    async getAction()
    {
        let data;
        if (this._guid)
        {
            let data = await this._judgerModel.where({'PK_JudgerGuid': this._guid}).find();
            return this.end(data);
        }

        data = await this._judgerModel.getAllJudger();
        return this.end(data);
    }


    /**
     * update resource
     * @return {Promise} []
     */
    async putAction()
    {
        if (!this._guid)
        {
            return this.error(52017, "guid不存在,请检查guid");
        }
        let postData = this.post('data');
        if (think.isEmpty(postData))
        {
            return this.error(52001, "提交的表单信息错误,请检查数据格式,上传数据格式为{'data',yourJsonData},如 : $.post(\"http://127.0.0.1:8091/apiv1/competition\",{'data':yourJsonData}),yourJsonData为你要上传的数据包");
        }
        let postDataObj;
        try
        {
            postDataObj = JSON.parse(postData);
        } catch (e)
        {
            //记录日志
            return this.error(52001, "提交的表单信息错误,请检查数据格式,上传数据格式为{'data',yourJsonData},如 : $.post(\"http://127.0.0.1:8091/apiv1/competition\",{'data':yourJsonData}),yourJsonData为你要上传的数据包");
        }

        let service = this._judgerService.getJudgerData(postDataObj);
        if (service.isErr === false)
        {
            let dbRes = await this._judgerService.updateData(this._guid);
            return this.error(dbRes.errNo, dbRes.errMsg);
        }
        return this.error(service.errNo, service.errMsg);
    }


    /**
     * delete resource
     * @return {Promise} []
     */
    async deleteAction()
    {
        if (!this._guid)
        {
            return this.error(52019, '删除评委时的guid为空,请检查guid')
        }
        let res = await this._judgerModel.where({'PK_JudgerGuid': this._guid}).update({'IsDelete': "已删除"});

        if (res === 1)
        {
            return this.error(0, "删除成功");
        } else
        {
            return this.error(52020, "主键为该guid的评委不存在或该条记录已修改,请检查guid");
        }
    }


    /**
     * 管理员登陆
     * @returns {Promise.<*>}
     */
    async judgerLoginAction()
    {
        let postData = think.stringToJson(this.post('data'));
        if (postData === false)
        {
            return this.end(52004, '提交的表单信息错误,该数据必须为json格式,请检查数据格式,上传数据格式为{\'data\',yourJsonData},如 : $.post("http://127.0.0.1:8091/apiv1/competition",{\'data\':yourJsonData}),yourJsonData为你要上传的数据包');
        }
        //验证评委是否是当前比赛
        let cpguid = postData.cpguid;

        this.stepsModel = this.model('apiv1/competitionSteps');
        //所有流程
        let cpsteps = await this.stepsModel.getCPStepsByCPGuid(cpguid);

        //获取judgers
        let judgers = [];
        for (let i = 0; i < cpsteps.length; i++)
        {
            let t = cpsteps[i].competitionstepstojudger;
            for (let j = 0; j < t.length; j++)
            {
                judgers.push(t[j]['FK_JudgerGuid']);
            }


        }

        //查询用户信息
        let uname = postData.uname;
        let pwd = postData.pwd;

        let judgerData = await this._judgerModel.where({
            UserName: uname,
            Password: pwd
        }).find();
        if (!think.in_array(judgers, judgerData['PK_JudgerGuid'])) return this.responseEmpty();


        if (think.isEmpty(judgerData))
        {
            this.info(10000);
        } else
        {
            this.responseQuery(judgerData);
        }

    }


    async jrstepAction()
    {
        let steps = await this._judgerModel.getStepsByJrguid(this._guid);
        let stesGuids=[];
        steps.forEach(function (val)
        {
            stesGuids.push(val.FK_CompetitionStepsGuid);
        });
        if(think.isEmpty(stesGuids)) {

            return this.responseEmpty();
        }
        let judgerWorks=await this.stepsModel.where({PK_CompetitionStepsGuid:['in',stesGuids]}).select();
        return this.responseQuery(judgerWorks);
    }
};